home *** CD-ROM | disk | FTP | other *** search
- /* LongArrayStats ------------------------------------------------
- *
- * Calculate various statistics for arrays of longs.
- *
- * Copyright (c) 1993 Bill Karsh.
- * All rights reserved.
- *
- */
-
-
- #pragma options( honor_register, !assign_registers )
-
-
- #include "LongArrayStats.h"
- #include <math.h>
-
-
-
-
-
-
- /* LongArrayMinMax -----------------------------------------------
- *
- * Calculate min and max values.
- *
- */
-
- void LongArrayMinMax(
- register long *dp,
- register long N,
- long *min,
- long *max )
- {
- register long mx = 0x80000000, mn = 0x7fffffff, d;
-
- if( !N ) {
- *min = *max = 0;
- return;
- }
-
- do {
- d = *dp++;
- if( d < mn ) mn = d;
- if( d > mx ) mx = d;
- } while( --N );
-
- *min = mn;
- *max = mx;
- }
-
-
- /* LongArrayMeanDev ----------------------------------------------
- *
- * Calculate array's mean and standard deviation.
- *
- */
-
- void LongArrayMeanDev(
- register long *dp,
- long N,
- double *mean,
- double *sd )
- {
- register long n = N, d;
- register double sumX, sumX2;
-
- sumX = sumX2 = 0;
-
- if( n ) {
-
- do {
- d = *dp++;
- sumX += d;
- sumX2 += d * d;
- } while( --n );
-
- n = N;
-
- sumX = sumX / n;
- sumX2 = sumX2 / n - sumX * sumX;
- if( n > 1 ) sumX2 *= n / (n - 1);
- sumX2 = sqrt( sumX2 );
- }
-
- *mean = sumX;
- *sd = sumX2;
- }
-
-
- /* LongArrayBin --------------------------------------------------
- *
- * Bin data array into a bins array.
- *
- * If input bins is nil, this routine allocates the bins array.
- *
- */
-
- long *LongArrayBin(
- register long *data,
- register long N,
- register long min,
- long max,
- long *bins,
- register long nBins )
- {
- register long *b;
- register long n, span;
-
- if( !bins )
- bins = (long*)NewPtr( sizeof(long)*(nBins + 1) );
-
- if( !(b = bins) ) goto exit;
-
- // zero bins array
-
- n = nBins + 1;
-
- do {
- *b++ = 0;
- } while( --n );
-
- // bin data
-
- b = bins;
-
- if( span = max - min ) {
-
- do {
- b[((*data++ - min) * nBins) / span]++;
- } while( --N );
-
- b[nBins - 1] += b[nBins];
- }
- else
- b[0] = N;
-
- exit:
- return bins;
- }
-
-
- /* LongArrayGetMaxBin --------------------------------------------
- *
- * Using an array {in} of data, and its corresponding array
- * {bins}, return in array {out}, only those data falling
- * in maximum height bin.
- *
- * in and out can be the same array.
- *
- */
-
- void LongArrayGetMaxBin(
- long *in,
- register long nIn,
- register long min,
- long max,
- long *bins,
- register long nBins,
- long *out,
- long *nOut )
- {
- register long *insert, *look;
- register long newN, maxCounts;
- short maxBinNum, iBin, pad;
-
- // quick exits
-
- if( !nIn ) {
- *nOut = 0;
- return;
- }
-
- if( max == min ) {
- *nOut = nIn;
- if( in != out ) {
- do {
- *out++ = *in++;
- } while( --nIn );
- }
- return;
- }
-
- // find max bin
-
- maxBinNum = 0;
- maxCounts = -1;
- look = bins;
-
- for( iBin = 0; iBin < (short)nBins; iBin++, look++ ) {
- if( *look > maxCounts ) {
- maxCounts = *look;
- maxBinNum = iBin;
- }
- }
-
-
- // replace data with data in max bin
-
- newN = 0;
- look = in;
- insert = out;
- max -= min;
-
- if( maxBinNum == nBins - 1 ) { // edge condition
-
- do {
- if( ((*look - min)*nBins)/max >= maxBinNum ) {
- *insert++ = *look;
- newN++;
- }
- look++;
- } while( --nIn && newN < maxCounts );
- }
- else {
-
- do {
- if( ((*look - min)*nBins)/max == maxBinNum ) {
- *insert++ = *look;
- newN++;
- }
- look++;
- } while( --nIn && newN < maxCounts );
- }
-
- *nOut = newN;
- }
-
-
-